#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>


template<class T>
class shared_ptr
{
public:
	shared_ptr(T* ptr = nullptr):ptr_(ptr),count_(new int(1))
	{}

	shared_ptr(const shared_ptr& s)
	{
		ptr_ = s.ptr_;
		count_ = s.count_;
	}

	~shared_ptr()
	{
		if (--(*count_) == 0)
		{
			delete ptr_;
			ptr_ = nullptr;
			delete count_;
			count_ = nullptr;
		}
	}

	shared_ptr& operator=(const shared_ptr& s)
	{
		if (this != &s)
		{
			if (--(*count_) == 0)
			{
				delete ptr_;
				delete count_;
			}

			ptr_ = s.ptr_;
			count_ = s.count_;
		}

		return *this;
	}

	T* operator->()
	{
		return ptr_;
	}

	T& operator*()
	{
		return *ptr_;
	}

private:
	T* ptr_;
	int* count_;
};


int main()
{
	shared_ptr<int> a(new int(10));
	shared_ptr<int> b = a;

	std::cout << *b << std::endl;
	std::cout << *a << std::endl;
	shared_ptr<int> c(new int(15));

	b = c;
	
	std::cout << *b << std::endl;

	return 0;
}